﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Xml;
using System.Xml.Linq;

public class htin
{
    public static string[] P_DOC_BM(string b_mau)
    {
        XmlDocument document = new XmlDocument();
        document.Load(HttpContext.Current.Server.MapPath(b_mau));
        string outerXml = document.OuterXml;
        string str2 = "";
        P_DOC_INDEX(ref str2, outerXml);
        if (str2 == "")
        {
            return new string[0];
        }
        return str2.Split(new char[] { ',' });
    }

    private static void P_DOC_INDEX(ref string b_bien, string b_mau)
    {
        int index = b_mau.IndexOf("w:type=\"Word.Bookmark.Start\" w:name=\"");
        if (index > 0)
        {
            index += "w:type=\"Word.Bookmark.Start\" w:name=\"".Length;
            string str = b_mau.Substring(index, b_mau.Length - index);
            int length = str.IndexOf("/>") - 2;
            string str2 = str.Substring(0, length);
            if (b_bien == "")
            {
                b_bien = str2;
            }
            else
            {
                b_bien = b_bien + "," + str2;
            }
            if (str.Length > 0)
            {
                P_DOC_INDEX(ref b_bien, str);
            }
        }
    }

    public static void P_MO(Page b_f, string b_url, string b_ten_bc, int b_width, int b_height, DataSet[] b_ds)
    {
        b_url = b_f.ResolveUrl(b_url);
        HttpContext.Current.Session["kq_ds"] = b_ds;
        ScriptManager.RegisterStartupScript(b_f, b_f.GetType(), "HTBC" + new Random().Next().ToString(), "window.open('" + b_url + "',true, 'resizable=yes,scrollbars=yes,height=" + b_height.ToString() + "px,width=" + b_width.ToString() + "px')", true);
    }

    public static void P_MO(Page b_f, string b_url, string b_ten_bc, int b_width, int b_height, DataSet b_ds)
    {
        b_url = b_f.ResolveUrl(b_url);
        HttpContext.Current.Session["kq_ds"] = b_ds;
        ScriptManager.RegisterStartupScript(b_f, b_f.GetType(), "HTBC" + new Random().Next().ToString(), "window.open('" + b_url + "',true, 'resizable=yes,scrollbars=yes,height=" + b_height.ToString() + "px,width=" + b_width.ToString() + "px')", true);
    }


    public static void P_XUAT_EXCEL(string b_mau, ref string b_out, string b_ten, DataSet b_ds)
    {
        string filename = HttpContext.Current.Server.MapPath(b_mau + b_ten + ".xml");
        b_out = b_out + b_ten + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".xls";
        string str2 = HttpContext.Current.Server.MapPath(b_out);
        XmlDocument document = new XmlDocument();
        document.Load(filename);
        CExcelMLFiller filler = new CExcelMLFiller(b_ds, document.OuterXml);
        if (!filler.OperationFailed)
        {
            filler.Transform();
            if (!filler.OperationFailed)
            {
                goto Label_00FC;
            }
            IEnumerator enumerator = filler.ErrorList.GetEnumerator();
            {
                while (enumerator.MoveNext())
                {
                    string current = (string) enumerator.Current;
                    return;
                }
                goto Label_00FC;
            }
        }
        IEnumerator enumerator2 = filler.ErrorList.GetEnumerator();
        {
            while (enumerator2.MoveNext())
            {
                string text2 = (string) enumerator2.Current;
                return;
            }
        }
    Label_00FC:
        filler.ExcelMLDocument.Save(str2);
    }

   

    public static void P_XUAT_WORD(string b_mau, ref string b_out, string b_ten, DataSet b_ds)
    {
        string filename = HttpContext.Current.Server.MapPath(b_mau + b_ten + ".xml");
        b_out = b_out + b_ten + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".doc";
        string str2 = HttpContext.Current.Server.MapPath(b_out);
        XmlDocument document = new XmlDocument();
        document.Load(filename);
        CExWordMLFiller filler = new CExWordMLFiller(b_ds, document.OuterXml);
        if (!filler.OperationFailed)
        {
            filler.Transform();
            if (!filler.OperationFailed)
            {
                goto Label_00FC;
            }
            IEnumerator enumerator = filler.ErrorList.GetEnumerator();
            {
                while (enumerator.MoveNext())
                {
                    string current = (string) enumerator.Current;
                    return;
                }
                goto Label_00FC;
            }
        }
        IEnumerator enumerator2 = filler.ErrorList.GetEnumerator();
        {
            while (enumerator2.MoveNext())
            {
                string text2 = (string) enumerator2.Current;
                return;
            }
        }
    Label_00FC:
        filler.WordMLDocument.Save(str2);
    }

    public static void P_XUAT_WORD_MAU(string b_mau, ref string b_out, string b_ten, DataTable b_dt)
    {
        try
        {
            int num;
            b_mau = b_mau + b_ten + ".xml";
            b_out = b_out + b_ten + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".doc";
            XmlDocument document = new XmlDocument();
            document.Load(HttpContext.Current.Server.MapPath(b_mau));
            string outerXml = document.OuterXml;
            int length = outerXml.IndexOf("<wx:sect>") + 9;
            string str2 = outerXml.Substring(0, length);
            int index = outerXml.IndexOf("<w:sectPr>");
            string str3 = outerXml.Substring(index, outerXml.Length - index);
            string str4 = "";
            string str5 = outerXml;
            DataTable table = new DataTable();
            for (num = 0; num < b_dt.Columns.Count; num++)
            {
                table.Columns.Add(b_dt.Columns[num].ColumnName.ToLower());
            }
            table.AcceptChanges();
            table.TableName = b_dt.TableName;
            for (num = 0; num < b_dt.Rows.Count; num++)
            {
                string str6;
                outerXml = str5;
                DataSet dsData = new DataSet();
                table.Clear();
                table.Rows.Add(b_dt.Rows[num].ItemArray);
                dsData.Tables.Add(table.Copy());
                CExWordMLFiller filler = new CExWordMLFiller(dsData, outerXml);
                if (!filler.OperationFailed)
                {
                    filler.Transform();
                    if (!filler.OperationFailed)
                    {
                        goto Label_01DD;
                    }
                    IEnumerator enumerator = filler.ErrorList.GetEnumerator();
                    {
                        while (enumerator.MoveNext())
                        {
                            string current = (string) enumerator.Current;
                            return;
                        }
                        goto Label_01DD;
                    }
                }
                IEnumerator enumerator2 = filler.ErrorList.GetEnumerator();
                {
                    while (enumerator2.MoveNext())
                    {
                        string text2 = (string) enumerator2.Current;
                        return;
                    }
                }
            Label_01DD:
                str6 = filler.WordMLDocument.OuterXml;
                int num4 = str6.IndexOf("<w:sectPr>");
                str4 = str4 + str6.Substring(length, num4 - length) + "<w:p><w:r><w:rPr><w:sz w:val=\"28\"/><w:sz-cs w:val=\"28\"/></w:rPr><w:br w:type=\"page\"/></w:r></w:p>";
            }
            XDocument.Parse(str2 + str4 + str3).Save(HttpContext.Current.Server.MapPath(b_out));
        }
        catch (Exception exception)
        {
            throw new Exception(exception.ToString());
        }
    }
}

